package legumes.client.ui.widgets.form.item;

import legumes.shared.domain.CivilityEnum;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.validator.CustomValidator;

public class CivilityEnumFormItem extends SelectItem {

	private CivilityEnum selectedCivility;
	
	public CivilityEnumFormItem(){
		super("CivilityEnumFormItem", "Civilité");
		setValue("Loading...");
		setShowTitle(true);
		setWidth(200);
		setTextAlign(Alignment.LEFT);
		setValidateOnExit(true);
		setValidateOnChange(true);
        setValidators(new CustomValidator(){

			@Override
			protected boolean condition(Object value) {
				CivilityEnum civEnum = CivilityEnum.fromDisplayName(value.toString());
				selectedCivility = civEnum;
				return selectedCivility != null;
			}
			
		});
        String [] civilities = CivilityEnum.getDisplayNames();
        setValueMap( civilities );
		setValue(civilities[0]);
		selectedCivility = CivilityEnum.fromDisplayName(civilities[0]);
	}
	
	public CivilityEnum getCivilityEnum(){
		return CivilityEnum.fromDisplayName( getValueAsString() );
	}
}
